<?php

namespace App\Models;

use App\Models\Rbac\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class WarehouseOrder extends BaseModel
{
    use HasFactory;
    protected $table = 'warehouse_order';
    // 允许批量操作的字段
    protected $fillable = [

        'clearing_form',
        'warehouse_id',
        'buyer',
        'supply_order_number',
        'warehouse_keeper',
        'finance',
        'prepared_by',
        'warehouse_entry_time',
        'remark',
        'num',
        'money',
        'type',
        'created_at',
        'updated_at'
    ];

    public function scopeSearch($query,$params)
    {
        $query->where('type',$params['type']??1);
        //供应单号
        if(!empty($params['supply_order_number'])){
            $query->where('supply_order_number',$params['supply_order_number']);
        }
        //入库时间
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query->whereBetween('warehouse_entry_time',[$params['start_time'],$params['end_time']]);
        }
        return $query;
    }


    //库房
    public function warehouse(){
        return $this->hasOne(Warehouse::class,'id','warehouse_id');
    }
    //采购人
    public function buyers(){
        return $this->hasOne(User::class,'id','buyer');
    }
    //仓库管理员
    public function warehouse_keepers(){
        return $this->hasOne(User::class,'id','warehouse_keeper');
    }
    //财务
    public function finances(){
        return $this->hasOne(User::class,'id','finance');
    }
    //制单人
    public function prepared_bys(){
        return $this->hasOne(User::class,'id','prepared_by');
    }
    public function child(){
        return $this->hasMany(WarehouseOrderChild::class,'warehouse_order_id','id');
    }




}
